package com.mmall.example.atomic;


import com.mmall.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * 线程安全的 --> AtomicReferenceFieldUpdater
 * Created by megan on 2018/3/18.
 */
@Slf4j
@ThreadSafe
public class AtomicReferenceFieldUpdaterExample {

    private static AtomicIntegerFieldUpdater<AtomicReferenceFieldUpdaterExample> updater =
            AtomicIntegerFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterExample.class, "count");

    @Getter
    private volatile int count = 100;


    public static void main(String[] args) {

        AtomicReferenceFieldUpdaterExample example =  new AtomicReferenceFieldUpdaterExample();

        if(updater.compareAndSet(example,100,200)){
            log.info("update success 1 , {}",example.getCount());
        }

        if(updater.compareAndSet(example,100,200)){
            log.info("update success 2 , {}",example.getCount());
        }else{
            log.info("update failed ,{}",example.getCount());
        }
    }

}
